package cn.xopencode.appletweb.controller.user;


import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.userservice.rpc.evaluate.EvaluateRpc;
import cn.xopencode.userservice.rpc.evaluate.dto.*;
import cn.xopencode.web.core.util.CommonWebUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "员工自评信息管理")
@RestController
@RequestMapping("/evaluate")
@Validated
public class EvaluateController {

    @Autowired
    private HttpServletRequest request;

    @JsonFormat
    @DubboReference(version = "${dubbo.consumer.EvaluateRpc.version}")
    private EvaluateRpc evaluateRpc;

    @ApiOperation(value = "查询单个员工自评信息")
    @GetMapping("/get/{evaluateId}")
    public CommonResult<EvaluateDTO> getEvaluate(@RequestBody @PathVariable("evaluateId") String evaluateId) {
        return evaluateRpc.getEvaluate(evaluateId);
    }

    @ApiOperation(value = "员工自评列表分页查询")
    @PostMapping("/list")
    public CommonResult<PageResult<EvaluateDTO>> findEvaluateList(@RequestBody EvaluatePageDTO evaluatePageDTO) {
        return evaluateRpc.findEvaluateList(evaluatePageDTO);
    }

    @ApiOperation(value = "新建员工自评")
    @PostMapping("/create")
    public CommonResult<String> createEvaluate(@RequestBody CreateEvaluateDTO createEvaluateDTO) {
        createEvaluateDTO.setUserId(CommonWebUtil.getUserId(request));
        return evaluateRpc.createEvaluate(createEvaluateDTO);
    }


    @ApiOperation(value = "批量更新员工自评信息")
    @PostMapping("/update")
    public CommonResult<Integer> updateEvaluate(@RequestBody List<UpdateEvaluateDTO> updateEvaluateDTOList) {
        String userId = CommonWebUtil.getUserId(request);
        updateEvaluateDTOList.forEach(updateEvaluateDTO -> {
            updateEvaluateDTO.setUserId(userId);
        });
        return evaluateRpc.updateEvaluate(updateEvaluateDTOList);
    }

    @ApiOperation(value = "批量更改员工自评属性")
    @PostMapping("/update/pro")
    public CommonResult<Integer> deleteEvaluate(@RequestBody List<UpdatePropertyDTO> updatePropertyDTOList) {
        String userId = CommonWebUtil.getUserId(request);
        updatePropertyDTOList.forEach(updatePropertyDTO -> {
            updatePropertyDTO.setUserId(userId);
        });
        return evaluateRpc.updateProperty(updatePropertyDTOList);
    }
}
